﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2009 ThinkWide
+-------------------------------------------------------------
项目: ORMapping服务类


版本: 1


创建日期:2010-7-3


作者: 章称

+-------------------------------------------------------------
描述:z

更新:   2010-7-3

ToDo:

***************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using MF.Data.SupplyInterface;

namespace MF.Data.ORMapping
{

    /// <summary>
    /// ORM-Store基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public abstract class MFActiveStoredBase<T> : MarshalByRefObject where T : MFActiveStoredBase<T>, new()
    {

        private MFDataParameters parameters;

        public string ParameterKey { get; set; }

        public MFActiveStoredBase()
        {
            parameters = new MFDataParameters();
            DefaultCommandTimeOut = 30;
            ParameterKey="@";
        }

        public int DefaultCommandTimeOut { get; set; }

        public MFDBResuleDataSetInfo ResuleInfo { get; set; }

        public MFDataParameters Parameters
        {
            get { return parameters; }
        }

        public bool SaveParameters()
        {
            Type type = typeof(T);

            foreach (PropertyInfo pInfo in type.GetProperties())
            {

                Type pType = pInfo.PropertyType;

                foreach (object APropertyAtt in pInfo.GetCustomAttributes(false))
                {
                    if (APropertyAtt is MFStoredParameterAttribute)
                    {
                        MFStoredParameterAttribute AParameterAttribute = (MFStoredParameterAttribute)APropertyAtt;
                        string parameterName=string.Empty;

                        if (string.IsNullOrEmpty(AParameterAttribute.ParameterName))
                            parameterName =ParameterKey+ pInfo.Name;
                        else
                            parameterName = ParameterKey+AParameterAttribute.ParameterName;

                        MFDataParameter param=Parameters[parameterName];

                        pInfo.SetValue(this,param.Value,null);

                       
                    }
                }

            }

            return true;
        }

        public bool LoadParameters()
        {
            Type type = typeof(T);
            Parameters.Clear();
            foreach (PropertyInfo pInfo in type.GetProperties())
            {

                Type pType = pInfo.PropertyType;

                foreach (object APropertyAtt in pInfo.GetCustomAttributes(false))
                {
                    if (APropertyAtt is MFStoredParameterAttribute)
                    {
                        MFStoredParameterAttribute AParameterAttribute = (MFStoredParameterAttribute)APropertyAtt;
                        MFDataParameter param=new MFDataParameter();
                        param.Size=AParameterAttribute.Size;

                        if (string.IsNullOrEmpty(AParameterAttribute.ParameterName))
                            param.ParameterName = ParameterKey + pInfo.Name;
                        else
                            param.ParameterName = ParameterKey+AParameterAttribute.ParameterName;

                        param.Direction = AParameterAttribute.Direction;
                        param.DbType = AParameterAttribute.DbType;
                        param.Value = pInfo.GetValue(this,null);

                        Parameters.Add(param);
                    }
                }

            }

            return true;
        }

        public string GetStoredProcedureName()
        {
            Type type = typeof(T);
            string sStoredProcedureName = typeof(T).Name;

            foreach (object AClassAtt in type.GetCustomAttributes(false))
            {
                if (AClassAtt is MFStoredAttribute)
                {
                    sStoredProcedureName = (AClassAtt as MFStoredAttribute).StoredProcedureName;
                    break;
                }

            }

            return sStoredProcedureName;
        }

        public virtual bool Execute()
        {
            LoadParameters();
            MFDBResuleInfo Info = MFORMappingGlobe.DataAdapter.ExecuteSQL(GetStoredProcedureName(), CommandType.StoredProcedure, Parameters, DefaultCommandTimeOut);
            ResuleInfo = new MFDBResuleDataSetInfo(); 
            ResuleInfo.Assign(Info);
            if (ResuleInfo.DataParameters!=null)
                parameters = ResuleInfo.DataParameters;
            SaveParameters();

            return Info.IsSuccess;
        }

        public virtual bool ExecuteDataSet()
        {
            LoadParameters();
            MFDBResuleDataSetInfo Info = MFORMappingGlobe.DataAdapter.ExecuteSQLDataSet(GetStoredProcedureName(), CommandType.StoredProcedure, Parameters, DefaultCommandTimeOut);
            ResuleInfo = Info;
            if (ResuleInfo.DataParameters != null)
                parameters = ResuleInfo.DataParameters;
            SaveParameters();
            return Info.IsSuccess; ;
        }

        public virtual List<L> QueryList<L>(int index)
        {
            if (ExecuteDataSet())
            {
                if (ResuleInfo.ResultDataSet != null && ResuleInfo.ResultDataSet.Tables.Count > 0)
                {

                    return MFDataTransfer.GetList<L>(ResuleInfo.ResultDataSet.Tables[index]);
                }
                return null;
            }
            else
            {
                return null;
            }

        }

        public virtual List<L> QueryList<L>()
        {

            return QueryList<L>(0);
        }
    }



    
}
